//
//  QSUserAccountViewModel.m
//  SinaWeiBo
//
//  Created by apple on 16/6/4.
//  Copyright © 2016年 apple. All rights reserved.
//

#import "QSUserAccountViewModel.h"
#import "QSUserAccount.h"
#import "QSUserAccount.h"

@implementation QSUserAccountViewModel

- (instancetype)init
{
    self = [super init];
 
    // 获取用户信息
    self.account = [[QSUserAccount alloc] loadAccount];
    
    return self;
}

/**
 *  重写登录标记的get方法
 */
- (BOOL)isUserLongin
{
    return self.account.access_token != nil;
}

/**
 *  请求用户令牌(token)
 */
- (void)loadAccessToken:(NSString *)code finish:(finish)finish
{
    // 创建网络工具类的单例
    QSNetworkTool *shareNetTool = [QSNetworkTool shareNetworkInstance];
    
    // url
    NSString *urlStr = @"https://api.weibo.com/oauth2/access_token";
    
    // 参数字典
    NSDictionary *paraDict = @{@"client_id":kCLIENT_ID, @"client_secret":kCLIENT_SECRET, @"grant_type":@"authorization_code", @"code":code, @"redirect_uri":kREDIRECT_URI};
    
    // 经常出现的一个错误
    //failed: unacceptable content-type: text/html  text/plain
    //AFN 默认只支持以下几种数据格式的解析 @"application/json", @"text/json", @"text/javascript"
    NSMutableSet *acceptTypesSet = [NSMutableSet set];
    acceptTypesSet = (NSMutableSet*)shareNetTool.afManager.responseSerializer.acceptableContentTypes;
    acceptTypesSet = (NSMutableSet*)[acceptTypesSet setByAddingObject:@"text/plain"];
    [shareNetTool.afManager.responseSerializer setAcceptableContentTypes:acceptTypesSet];
    
    // post请求
    [shareNetTool requestWebWithParaWithURL:urlStr Parameter:paraDict Finish:^(id result) {
        // 请求成功
        QSLog(@"%@", result);
        // 创建用户对象
        QSUserAccount *userAccount = nil;
        
        // 判断获取到的数据是字典还是数组
        if ([result isKindOfClass:[NSDictionary class]]) {
            // 字典转模型, 保存用户数据
            userAccount = [QSUserAccount userAccountWithDict:result];
        }
        
        QSLog(@"登录成功");
        // 加载用户信息
        //        [self loadUserInfoWithToken:token andUid:uid];
        [self loadUserInfoWithAccount:userAccount finish:finish];
        
    } Error:^(NSURLSessionDataTask *task, NSError *error, NSString *description) {
        // 请求失败
        QSLog(@"%@", error);
        [QSSVProgressTool showFailStatus:@"您的网络不给力"];
    }];
}

/**
 *  加载用户信息
 *
 *  @param Token 令牌
 *  @param uid   用户ID
 */
//- (void)loadUserInfoWithToken:(NSString *)token andUid:(NSString *)uid
- (void)loadUserInfoWithAccount:(QSUserAccount *)userAcc finish:(finish)finish
{
    // 创建网络工具类的单例
    QSNetworkTool *shareNetTool = [QSNetworkTool shareNetworkInstance];
    
    // getUrl
    NSString *getUrl = @"https://api.weibo.com/2/users/show.json";
    
    // 参数
    NSDictionary *paraDict = @{@"access_token":userAcc.access_token, @"uid":userAcc.uid};
    
    // get
    [shareNetTool getWithURL:getUrl Parameter:paraDict Finish:^(id result) {
        // 请求成功
        QSLog(@"%@", result);
        
        // 判断获取到的数据是字典还是数组
        if ([result isKindOfClass:[NSDictionary class]]) {
            // 保存用户数据到模型中
            userAcc.avatar_large = result[@"avatar_large"];
            userAcc.name = result[@"name"];
            
            // 保存数据到沙盒
            [userAcc saveAccount];
            finish(nil);
        }
        
    } Error:^(NSURLSessionDataTask *task, NSError *error, NSString *description) {
        // 请求失败
        QSLog(@"%@", error);
        finish(error);
        [QSSVProgressTool showFailStatus:@"您的网络不给力"];
    }];
}

@end
